package com.xzq.erp.controller;


import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.MenuDTO;
import com.xzq.erp.domain.query.MenuPageQuery;
import com.xzq.erp.domain.vo.MenuVO;
import com.xzq.erp.service.IMenuService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统菜单表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/menu")
@Tag(name = "菜单相关接口")
@RequiredArgsConstructor
public class MenuController {

    private final IMenuService menuService;

    /**
     * 获取所有菜单列表
     */
    @GetMapping
    @Operation(summary = "获取所有菜单列表")
    @ApiOperationLog(description = "获取所有菜单列表")
    @PreAuthorize("hasAuthority('sys:menu:list') || hasRole('ADMIN')")
    public Response<List<MenuVO>> getMenuList(@ParameterObject @Validated MenuPageQuery query) {
        return Response.success(menuService.getMenuList(query));
    }
    
    /**
     * 获取当前用户菜单列表
     */
    @GetMapping("/current")
    @Operation(summary = "获取当前用户菜单列表")
    @ApiOperationLog(description = "获取当前用户菜单列表")
    public Response<List<MenuVO>> getCurrentUserMenuList() {
        return Response.success(menuService.getCurrentUserMenuList());
    }
    
    /**
     * 根据角色ID获取菜单列表
     */
    @GetMapping("/role/{roleId}")
    @Operation(summary = "根据角色ID获取菜单列表")
    @ApiOperationLog(description = "根据角色ID获取菜单列表")
    @PreAuthorize("hasAuthority('sys:role:menu:list') || hasRole('ADMIN')")
    public Response<List<MenuVO>> getMenuListByRoleId(
            @Parameter(description = "角色ID") @PathVariable Integer roleId) {
        return Response.success(menuService.getMenuListByRoleId(roleId));
    }

    /**
     * 添加菜单
     */
    @PostMapping
    @Operation(summary = "添加菜单")
    @ApiOperationLog(description = "添加菜单")
    @PreAuthorize("hasAuthority('sys:menu:add') || hasRole('ADMIN')")
    public Response<Void> addMenu(@RequestBody @Validated MenuDTO menuDTO) {
        menuService.save(menuDTO);
        return Response.success();
    }

    /**
     * 修改菜单
     */
    @PutMapping
    @Operation(summary = "修改菜单")
    @ApiOperationLog(description = "修改菜单")
    @PreAuthorize("hasAuthority('sys:menu:edit') || hasRole('ADMIN')")
    public Response<Void> updateMenu(@RequestBody @Validated MenuDTO menuDTO) {
        menuService.updateMenu(menuDTO);
        return Response.success();
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{type}/{id}")
    @Operation(summary = "删除菜单")
    @ApiOperationLog(description = "删除菜单")
    @PreAuthorize("hasAuthority('sys:menu:delete') || hasRole('ADMIN')")
    public Response<Void> deleteMenu(@Parameter(description = "菜单类型") @PathVariable("type") Integer type,@Parameter(description = "菜单ID") @PathVariable("id") Integer id) {
        menuService.deleteMenu(id, type);
        return Response.success();
    }

}
